var requestAllImageListToFile = require("./requestAllImageListToFile.js");
var downloadImage = require("./downloadImage.js");
var eventproxy = require("eventproxy");
var async = require("async");

var ep = new eventproxy();

requestAllImageListToFile.requestAllImageList(requestAllImageListToFile.currentDateInt, function (message) {
    if(message) {
        requestAllImageListToFile.loadAllImagesFromFile(function(images) {
            startDownloadImage(images);
        });
    }
});

function startDownloadImage(images) {
    async.mapLimit(images, 20, function (imageDic, callback) {
        downloadImage.downloadImage(imageDic.image_url, imageDic.pub_time + "-" + imageDic.description + ".jpg", callback);
    }, function (err, result) {
        console.log('所有图片下载完成');
    });
}